var file_util = require('../public/file_util');
var path = require('path');
const Define = require('./ExportDefine')
const CSParser = require('./CSParser')
function CSExport() {
    this.parserList = []
}
CSExport.prototype.init = function (cur_path, copy_info) {
    this.copy_info = copy_info;
    this.cur_path = cur_path;
    var temp = path.join(cur_path, 'template', this.copy_info.lang, 'class.cs')
    // console.log(' temp ', temp)
    this.template = file_util.readFile(temp);
    var manager = path.join(cur_path, 'template', this.copy_info.lang, 'manager.cs')
    this.managerTemplate = file_util.readFile(manager)
}

CSExport.prototype.exportClassFile = function (export_dir, excelList) {
    let class_path = path.join(export_dir, this.copy_info.lang, Define.CLASS_FOLDER);
    file_util.createDir(class_path)
    let moduleFolders = {}
    for (let j = 0; j < this.parserList.length; j++) {
        const parser = this.parserList[j];
        if (parser.classObj) {
            let maPath = class_path;
            let moduleFolder = parser.getModuleName();
            if(moduleFolder){
                let folder = moduleFolders[moduleFolder]
                if(!folder){
                    folder = path.join(class_path , moduleFolder)
                    moduleFolders[moduleFolder] = folder;
                    file_util.createDir(folder)
                  
                }
                maPath = folder;
               
            }
            file_util.writeFile(path.join(maPath, parser.className + ".cs"), parser.classObj);
        }
        // temp += parser.fileNames + '\n\t'
    }
}
//导出数据表中定义的枚举值
CSExport.prototype.exportEnumDefine = function (export_dir, excelList) {
    let data = ''
    for (let j = 0; j < this.parserList.length; j++) {
        const parser = this.parserList[j];
        if (parser.enumDefine) {
            data += parser.enumDefine +  Define.CR
        }
    }
    let p = path.join(export_dir, this.copy_info.lang, 'EnumDefine.cs')
    file_util.writeFile(p, data);
}
CSExport.prototype.parse = function (excelList) {
    for (let index = 0; index < excelList.length; index++) {
        const excel = excelList[index];
        let sheetList = excel.getSheetList(this.copy_info.filter);
        for (let j = 0; j < sheetList.length; j++) {
            const sheet = sheetList[j];
            let parser = new CSParser(sheet)
            parser.parse(this.managerTemplate, this.template);
            this.parserList.push(parser)
        }
    }
}
CSExport.prototype.export = function (export_dir, excelList) {
    file_util.createDir(path.join(export_dir, this.copy_info.lang))
    this.parse(excelList)
    this.exportClassFile(export_dir, excelList)
    this.exportEnumDefine(export_dir, excelList)
    // this.exportFileName(export_dir, excelList)
    // this.exportDeclare(export_dir, excelList)
}

CSExport.prototype.copy = function (moduleName, export_path) {
    //将json文件复制到项目中
    let data_dest_path = this.copy_info.data_dest_path;
    if (data_dest_path) {
        var json_path = path.join(export_path, Define.CLIENT_FOLDER)
        var dest_json_path = data_dest_path
        //拷贝数据
        file_util.createDir(dest_json_path)

        file_util.copyDir(json_path, dest_json_path)

    }

    let lang_dest_path = this.copy_info.lang_dest_path
    if (lang_dest_path) {
        //拷贝语言文件
        var dest_lang_path = lang_dest_path
        file_util.createDir(dest_lang_path)
        var langFile = moduleName + "_lang_zh.json"
        // console.log('copy_path  ===', data_dest_path, " json_path ", json_path, 'langFile', langFile)
        file_util.copy(path.join(export_path, langFile), path.join(dest_lang_path, langFile))
    }


    let enum_dest_path = this.copy_info.enum_dest_path;
    if (enum_dest_path) {
        file_util.createDir(enum_dest_path)
        var EnumDefine = "EnumDefine.cs"
        file_util.copy(path.join(export_path, this.copy_info.lang, EnumDefine), path.join(enum_dest_path, EnumDefine))
        // var fileName = 'JsonDefine.cs'
        // file_util.copy(path.join(export_path, this.copy_info.lang, fileName), path.join(enum_dest_path, fileName))
    }
    // if (enum_dest_path) {
    //     file_util.createDir(enum_dest_path)
    //     file_util.copyDir(path.join(export_path, this.copy_info.lang, Define.CLASS_FOLDER),enum_dest_path)
    // }
}
module.exports = CSExport; 